(function() {
    'use strict';

    angular
        .module('byibserverApp')
        .controller('WcProductDialogController', WcProductDialogController);

    WcProductDialogController.$inject = ['$timeout', '$scope', '$stateParams', '$uibModalInstance', 'entity', 'WcProduct', 'WcIsurer', 'WcInsuranceCategory','FileUploader'];

    function WcProductDialogController ($timeout, $scope, $stateParams, $uibModalInstance, entity, WcProduct, WcIsurer, WcInsuranceCategory,FileUploader) {
        var vm = this;

        vm.wcProduct = entity;
        vm.clear = clear;
        vm.save = save;
        vm.wcisurers = WcIsurer.query();
        vm.wcinsurancecategories = WcInsuranceCategory.query();

        var uploader=$scope.uploader = new FileUploader({
            url: 'http://www.biyibibaoxian.com/java/api/up-file'
        });

        // 回调
        uploader.onSuccessItem = function(fileItem, response, status, headers) {
            console.info('onSuccessItem', fileItem, response, status, headers);
            vm.wcProduct.exemptionClauseFile=response.image_link;
        };
        uploader.onErrorItem = function(fileItem, response, status, headers) {
            console.info('onErrorItem', fileItem, response, status, headers);
        };


        var contract_uploader=$scope.contract_uploader = new FileUploader({
            url: 'http://www.biyibibaoxian.com/java/api/up-file'
        });

        // 回调
        contract_uploader.onSuccessItem = function(fileItem, response, status, headers) {
            console.info('onSuccessItem', fileItem, response, status, headers);
            vm.wcProduct.contract=response.image_link;
        };
        contract_uploader.onErrorItem = function(fileItem, response, status, headers) {
            console.info('onErrorItem', fileItem, response, status, headers);
        };



        console.log(uploader)

        // 过滤器
        // uploader.filters.push({
        //     name: 'customFilter',
        //     fn: function(item /*{File|FileLikeObject}*/, options) {
        //         return this.queue.length < 10;
        //     }
        // });

        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            if (vm.wcProduct.id !== null) {
                WcProduct.update(vm.wcProduct, onSaveSuccess, onSaveError);
            } else {
                WcProduct.save(vm.wcProduct, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('byibserverApp:wcProductUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }


    }
})();
